/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: tiny_array.c
 *
 * Purpose: dynamic array container implementation
 *
 * Developer:
 *   wen.gu , 2017-03-27
 *
 * TODO: 
 *
 ***************************************************************************/


#include <string.h>

#include "general_error.h"
#include "general_macro.h"

#include "vos_mem.h"

#include "tiny_array.h"




/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/
#define LOGE printf
#define LOGD printf
#define LOGW printf
/***************************************************************************
 *
 * data structure declaration
 *
 ***************************************************************************/
typedef struct my_array_s
{
	GU32 length; /**< how many count of element in array */
	GU32 capacity; /**< the total space of current array */
	void** base;  /**< the base adress if array */
	tfArrayElemFreeFunc_t elem_free; /**< the callback function for free element */
}my_array_t;

/***************************************************************************
 *
 * inner API declaration
 *
 ***************************************************************************/
static void tfArrayClear(my_array_t *ma)
{
	GU32 i = 0;
	for (; i < ma->length; i++) 
	{
		if (ma->elem_free)
		{
			ma->elem_free(*(ma->base + i));
		}
	}
	
	ma->length = 0;
}

static GS32 tfArrayExpandSpace(my_array_t *ma)
{
	GS32 ret = G_OK;
	size_t newSize = ma->capacity + (ma->capacity >> 1);
	void** newBase = (void **)realloc(ma->base, newSize * sizeof(void *));

	if (!newBase)
	{
		ma->base = newBase;
		ma->capacity = newSize;
	}
	else
	{
		LOGE("expand dynamic array capacity failed...\n");
		ret = G_ErrorInsufficientResources;
	}

	return ret;
}

/***************************************************************************
 *
 * API declaration
 *
 ***************************************************************************/
GPHD tfArrayCreate(GU32 length, tfArrayElemFreeFunc_t elem_free)
{
	my_array_t* ma = (my_array_t*)memAlloc(sizeof(my_array_t));

	if (ma)
	{
		memset(ma, 0, sizeof(*ma));
		ma->capacity = GMAX(length, DEFAULT_ARRAY_CAPACITY);

		ma->base = (void**)calloc(ma->capacity, sizeof(void*));

		GCHECK(ma->base);
		ma->elem_free = elem_free;
	}
	else
	{
		LOGE("alloc array instance failed\n");
	}

	return ma;
}

void tfArrayDestroy(GPHD array_hd)
{
	if (array_hd)
	{
		my_array_t* ma = (my_array_t*)array_hd;

		if (ma->base)
		{
			tfArrayClear(ma);
			free(ma->base);
		}

		memFree(array_hd);
	}
}

GS32 tfArrayGet(GPHD array_hd, GU32 index, void **value)
{
	GS32 ret = G_ErrorBadParameter;
	if (array_hd)
	{
		my_array_t* ma = (my_array_t*)array_hd;

		if (index <= ma->length)
		{
			*value = *(ma->base + index);
			ret = G_OK;
		}
	}

	return ret;

}
GS32 tfArraySet(GPHD array_hd, GU32 index, void *value)
{
	GS32 ret = G_ErrorBadParameter;
	if (array_hd)
	{
		my_array_t* ma = (my_array_t*)array_hd;

		if (index <= ma->length)
		{
			*(ma->base + index) = value;
			ret = G_OK;
		}
	}

	return ret;
}

GS32 tfArrayAppend(GPHD array_hd, void *value)
{
	GS32 ret = G_ErrorBadParameter;
	if (array_hd)
	{
		my_array_t* ma = (my_array_t*)array_hd;

		if (ma->length >= ma->capacity)
		{
			/* expand the general array capacity */
			ret = tfArrayExpandSpace(ma);
		}
		else
		{
			ret = G_OK;
		}

		if (G_OK == ret)
		{
			*(ma->base + ma->length) = value;
			ma->length++;
		}
		else
		{
			LOGE("expand dynamic array capacity failed...\n");
		}
	}

	return ret;
}

GS32 tfArrayRemove(GPHD array_hd, GU32 index)
{
	GS32 ret = G_ErrorBadParameter;
	if (array_hd)
	{
		my_array_t* ma = (my_array_t*)array_hd;
		if (index < ma->length)
		{
			GS32 i = 0;
			GS32 len = ma->length - 1;

			for (i = index; i < len; i++)
			{
				*(ma->base + i) = *(ma->base + i + 1);
			}

			ma->length--;
			ret = G_OK;
		}
		else
		{
			LOGE("index(%d) out of range(0 - %d)\n", index, ma->length);
			ret = G_ErrorOutOfRange;
		}
	}

	return ret;
}

GBOL tfArrayEmpty(GPHD array_hd)
{
	if (array_hd)
	{
		my_array_t* ma = (my_array_t*)array_hd;

		return (ma->length > 0)? GFALSE : GTRUE;
	}

	return GTRUE;
}

GU32 tfArrayElementCount(GPHD array_hd)
{
	if (array_hd)
	{
		my_array_t* ma = (my_array_t*)array_hd;

		return ma->length;
	}

	return 0;
}
